package com.hadoop.mapreduce.wordcount;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class WordCountReducer extends Reducer<Text,IntWritable,Text,IntWritable> {
    /**
     *生命周期：框架每传递进来一个kv 组，reduce方法被调用一次
     * @param key 是一组相同单词对应的KV的key
     * @param values
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        //定义一个计数器
        int count=0;
        //遍历这一组kv的所有v，累加到count中
        for (IntWritable intWritable:values
             ) {
            count+=intWritable.get();
        }
        context.write(key,new IntWritable(count));

    }
}
